package com.terrapages.wmsc4j.example;

import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;

import com.terrapages.wmsc4j.bean.BBox;
import com.terrapages.wmsc4j.cache.WMSCache;
import com.terrapages.wmsc4j.client.mosaic.MosaicWMSCClient;
import com.terrapages.wmsc4j.client.mosaic.MosaicWMSCResponse;
import com.terrapages.wmsc4j.layer.StaticLayer;
import com.terrapages.wmsc4j.layer.profiles.GlobalProfile;
import com.terrapages.wmsc4j.layer.tile.ImageMimeType;
import com.terrapages.wmsc4j.wms.WMSParameters;
import com.terrapages.wmsc4j.wms.WMSService;

/**
 * This is an example to show how to make Mosaic Maps.
 * Mosaic maps are abitary sized maps from a wmsc.
 * 
 * @author mvivian
 * @date 15/6/07
 */
public class MosaicExample {
	//The URL of the WMS service
	private static final String WMS_URL = "http://wmsc1.terrapages.net/getdevmap";
	//A query that works which WMSC4j will use to create other requests
	private static final String WMS_QUERY = 
		"LAYERS=UnprojectedStreet&" +
		"FORMAT=image/jpeg&" +
		"SERVICE=WMS&" +
		"VERSION=1.1.1&" +
		"REQUEST=GetMap&" +
		"STYLES=&" +
		"EXCEPTIONS=application/vnd.ogc.se_inimage&" +
		"SRS=EPSG:4326&" +
		"BBOX=138.515625,-34.980469,138.691406,-34.804687&" +
		"WIDTH=256&" +
		"HEIGHT=256";
	
	public static void main(String[] args) throws MalformedURLException 
	{
		/*-------------------------------------------,
		|    First we need the information about     |
		|    the layer and tiles and the cache       |
		`-------------------------------------------*/
		
		// Makes an UNPROJECTED Global Profile static layer
		// see http://wiki.osgeo.org/index.php/WMS_Tiling_Client_Recommendation#Unprojected_Profile for reference
		final StaticLayer layer = GlobalProfile.UNPROJECTED
			//static layer is the simplest form of layer
			.makeStaticLayer(
//				WMSCache means that you connect to this layers cache via WMS
				new WMSCache(
					//The WMS cache will connect to this particulat WMS service
					new WMSService(
							//Use this WMS service (Host and Path)
							new URL(WMS_URL),
							//make a WMSParameters object this way
							WMSParameters.parseQuery(WMS_QUERY)
					)
				),
				//How many resolutions are there
				20, 
				//What kind of images does the layer use
				ImageMimeType.JPEG,
				//Whats the name of the layer
				"UnprojectedStreet"
			);
		
		/*-----------------------------------,
		|    Now for the easy part           |
		`-----------------------------------*/
		
		//What do you want a map of?...How about the state of Victoria (AUS)
		BBox victoria = new BBox(140.9,-39.4,150,-33.9);
		//How big do you want it?
		Dimension thisBig = new Dimension(2000,1000);
		
		
		MosaicWMSCClient mapMaker = new MosaicWMSCClient();
		MosaicWMSCResponse response = mapMaker.getMap(layer, victoria, thisBig);
		
		/*----------------------------------------,
		|    How easy was that!                   |
		|    So this is what you have now ...     |
		`----------------------------------------*/
		

		@SuppressWarnings("unused")
		final BBox YOUR_NEW_IMAGE_BOUNDINGBOX = response.getBoundingBox();
		/*
		   Didn't I ask for Victoria?
		   Yes, and it did the best it could.
		   But you talking to a wmsc with set zoom levels
		   This is the closest BoundingBox it could get for the image size you requested
	    */
		
		final BufferedImage YOUR_MAP = response.getImage();
		/*
		   And heres what your really after....Yay!
	    */
		
		
		//Here, I'll save the map for you. 
		saveMap(YOUR_MAP);
		//Look for 'mosaic_example_*.png' in your OS temp directory.
	}
	
	
	/**
	 * Dont mind me, im just saving your map
	 */
	private static void saveMap(BufferedImage image)
	{
		String path = System.getProperty("java.io.tmpdir");
		String time = new SimpleDateFormat("hms").format(new Date());
		File file = new File(path + "/mosaic_example_"+ time +".png");
		try {
			ImageIO.write(image, "PNG", file);
		} catch (IOException e) {
			// Oops didn't work...who cares!
			e.printStackTrace();
		}
	}
}
